//
//  VisitingViewController.m
//  移动营销
//
//  Created by Hanen 3G 01 on 16/2/23.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "VisitingViewController.h"
#import "VisitTableViewCell.h"
#import "MyClientView.h"

#import "SelectViewController.h"
#import "MyTabBarController.h"
#import "NewVisitViewController.h"
#import "VisitDetailViewController.h"
#import "LoginViewController.h"
#import "MapClientViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "HEClander.h"
#import "DateTools.h"

#import "TopSegmentView.h"

#import "SelectPersonViewController.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"

#define NAVHEI 44


@interface VisitingViewController ()<NewVisitViewDelegate,SelectStaffViewDelegate23,VisitTableViewCellDelegate,HECalanderDelegate,SegViewDelegate,SelectStaffViewDelegate>{
    
    int calendarHei;
    float lastContentOffset;
    UILabel *endLabel;
    
    //测试数据
    NSArray *arr;
    NSArray *addarr;
    NSArray *starr;
    
    int statuIndex;
    int pagenum;
    int rowsnum;
    
    UIButton *endButt;
    NSMutableArray *dayArr1;
    NSMutableArray *dayArr2;
    HEClander *clander;
    int count;
    
    UIView *sectionLine1;
    UIView *sectionLine2;
    
    NSString *kid;
    
}
@property (nonatomic, strong) UISwipeGestureRecognizer *upSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *downSwipeGestureRecognizer;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *monthLabel;

@property(nonatomic) BOOL isManager;
@property(nonatomic) BOOL isMyVisit;
@property(nonatomic, strong) TopSegmentView *topBtnView;

@property (strong, nonatomic) UIView *backView;

/**
 *  我的拜访
 */
@property(nonatomic,strong)UITableView     *visitTableView;
@property(nonatomic,strong)UIView          *myVisitView;

@property(nonatomic,strong)NSArray *myVisitArray;

/**
 *  下属拜访
 */
@property(nonatomic,strong)UITableView     *otherVisitTableView;
@property(nonatomic,strong)UIView          *otherVisitView;

@property(nonatomic,strong)NSArray *otherVisitArray;

@property(nonatomic,strong) UILabel *splitLine;

@property(nonatomic,strong) UIView *planNumView;


//data
@property (nonatomic, strong) NSMutableArray *timelist1;
@property (nonatomic, strong) NSMutableArray *timelist2;

@property (nonatomic, assign) int allcount;
@property (nonatomic, assign) int daicount;
@property (nonatomic, assign) int yicount;
@property (nonatomic, strong) NSArray *daylist;

@property (nonatomic, assign) int allcount2;
@property (nonatomic, assign) int daicount2;
@property (nonatomic, assign) int yicount2;
@property (nonatomic, strong) NSArray *daylist2;//2--other

@property (nonatomic, assign) int i;

@property (nonatomic, strong) UILabel *planbLab;
@property (nonatomic, strong) UILabel *completedLab;
@property (nonatomic, strong) UILabel *notCompletedLab;


@property (nonatomic, strong) NSString *monthstart;
@property (nonatomic, strong) NSString *yearstart;
@property (nonatomic, strong) NSString *daystart;

@property (nonatomic, strong) NSString *month;
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *day;

@property (nonatomic, strong) NSDate   *date;

@end

@implementation VisitingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = NavBackColor;
    self.navigationItem.hidesBackButton =YES;
    
    
    self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_add" WithAction:@selector(clickToNewVisit)];
    
    NSString *type = [[[NSUserDefaults standardUserDefaults] objectForKey:@"type"] stringValue];
    statuIndex = -1;
    pagenum = 1;
    rowsnum = 10;
    kid = @"";
    if ([type isEqualToString:@"2"]) {
        _isManager = YES;
        _topBtnView = [[TopSegmentView alloc] initWithFrame:CGRectMake(0, 0, 170.0f, 30)];
        _topBtnView.delegate = self;
        _topBtnView.rightTitle = @"我的拜访";
        _topBtnView.leftTitle = @"下属拜访";
        _topBtnView.indicatorViewColor = [UIColor whiteColor];
        _topBtnView.firstIndex = 1;
        
        self.navigationItem.titleView = _topBtnView;
        
    }else{
        _isManager = NO;
        self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"我的拜访"];
    }
    
    _monthstart = [DateTool getMonth:[NSDate date]];
    _yearstart = [DateTool getYear:[NSDate date]];
    _daystart = [DateTool getDay:[NSDate date]];
    
    _month = [DateTool getMonth:[NSDate date]];
    _year = [DateTool getYear:[NSDate date]];
    _day = [DateTool getDay:[NSDate date]];
    
    _date = [NSDate date];
    _timelist1 = [NSMutableArray array];
    _timelist2 = [NSMutableArray array];
    dayArr1 = [NSMutableArray array];
    dayArr2 = [NSMutableArray array];
    
    [self addCalendarView];
    [self addPlanView];
    [self addTableView];
    
    [self reloadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TongZhi:) name:@"tongzhi" object:nil];
}

- (void)TongZhi:(NSNotification *)userinfo
{
    if ([userinfo.userInfo[@"refresh"] isEqualToString:@"refresh"]) {
        [self refreshData];
    }
}

#pragma mark --MyClientView delagte
- (void)segmentView:(TopSegmentView *)segment didScrollIndex:(NSInteger)index{
    if (index == 0) {
        
        self.navigationItem.rightBarButtonItem  = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_screen" WithAction:@selector(clickToselectUser)];
        //self.navigationItem.rightBarButtonItem = nil;
        _visitTableView.hidden = YES;
        _otherVisitTableView.hidden = NO;
        
        [_topBtnView.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_topBtnView.leftBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        endLabel = _planbLab;
        _planbLab.backgroundColor = [UIColor whiteColor];
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_SUB_VISIT_URL withArray:nil status:-1];
        [self getMonthListWithURL:GET_MONTH_SUB_VISIT_URL withYear:_year withMon:_month];
        
    }else if(index == 1){
        
        self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_add" WithAction:@selector(clickToNewVisit)];
        self.navigationItem.leftBarButtonItem  = nil;
        _visitTableView.hidden = NO;
        _otherVisitTableView.hidden = YES;
        [_topBtnView.rightBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        [_topBtnView.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_VISIT_URL withArray:nil status:-1];
        [self getMonthListWithURL:GET_MONTH_VISIT_URL withYear:_year withMon:_month];
        NSLog(@"22222222222222222");
    }
}
- (void)segmentView:(TopSegmentView *)segment didSelectIndex:(NSInteger)index withTitle:(NSString *)btnTitlte
{
    if (index == 0) {
        
        self.navigationItem.rightBarButtonItem  = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_screen" WithAction:@selector(clickToselectUser)];
        //self.navigationItem.rightBarButtonItem = nil;
        _visitTableView.hidden = YES;
        _otherVisitTableView.hidden = NO;
        [_topBtnView.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_topBtnView.leftBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        endLabel = _planbLab;
        _planbLab.backgroundColor = [UIColor whiteColor];
        
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_SUB_VISIT_URL withArray:nil status:-1];
        [self getMonthListWithURL:GET_MONTH_SUB_VISIT_URL withYear:_year withMon:_month];
        
    }else if(index == 1){
        
        self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_add" WithAction:@selector(clickToNewVisit)];
        self.navigationItem.leftBarButtonItem  = nil;
        _visitTableView.hidden = NO;
        _otherVisitTableView.hidden = YES;
        [_topBtnView.rightBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
        [_topBtnView.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_VISIT_URL withArray:nil status:-1];
        [self getMonthListWithURL:GET_MONTH_VISIT_URL withYear:_year withMon:_month];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    [super viewDidAppear:animated];
    //    [self.calendar reloadData]; // Must be call in viewDidAppear
}
- (void)viewWillAppear:(BOOL)animated
{
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, _visitTableView.centerY - self.view.centerY+45+64)];
    count++;
    if (count != 1) {
        [self reloadData];
        [_visitTableView setContentOffset:CGPointMake(0, 0) animated:NO];
        [_otherVisitTableView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, 0)];
}
- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma ----------------- HEClander Delegate ------------------
- (void)currentSelectedDateYear:(NSString *)Year Month:(NSString *)Month Day:(NSString *)Day
{
//    NSLog(@"--------%@-%@-%@  ++++++++++",Year,Month,Day);
    
    _monthstart = Month;
    _yearstart = Year;
    _daystart = Day;
    _year = Year;
    _month = Month;
    _day = Day;
    
    [SVProgressHUD showWithStatus:@"加载中"];
    [self reloadData];
    
}

- (void)currentSelectedDate:(NSString *)SingleSelected DoubleSelected:(NSString *)DoubleSelected Year:(NSString *)Year Month:(NSString *)Month
{
//    NSLog(@"--------%@  ++++++++++%@",SingleSelected,DoubleSelected);
    _yearstart = Year;
    _monthstart = Month;
    
    _year = Year;
    _month = Month;
    
    _daystart = SingleSelected;
    _day = DoubleSelected;
    
    [SVProgressHUD showWithStatus:@"加载中"];
    [self reloadData];
}

- (void)clanderDragUpAndDown
{
    [UIView animateKeyframesWithDuration:0.1 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
        _planNumView.frame = CGRectMake(0, self.calendar.height, KSCreenW, SECTIONHEIGHT);
        _splitLine.frame = CGRectMake(LEFTWIDTH, _planNumView.maxY-1, KSCreenW-LEFTWIDTH*2, 1);
        
        _visitTableView.frame =CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH- _splitLine.maxY-TabbarH-64);
        _otherVisitTableView.frame =CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH- _splitLine.maxY-TabbarH-64);
        
    } completion:nil];
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, _visitTableView.centerY - self.view.centerY+60+64)];
}

//日历
- (void)addCalendarView{
    
    clander = [[HEClander alloc] init];
    clander.frame = CGRectMake(0, 0, self.view.frame.size.width, 75+50*6*(KSCreenH/736.0));
    
    clander.HEClanderDelegate = self;
    
    clander.startBottomLabText = @"";
    
    clander.endBottomLabText = @"";
    
    [self.view addSubview:clander];
    self.calendar =  clander;
    
}

- (void)addPlanView{
    _planNumView = [[UIView alloc] initWithFrame:CGRectMake(0, self.calendar.height, KSCreenW, SECTIONHEIGHT)];
    _planbLab = [[UILabel alloc] initWithFrame:CGRectMake(0,0, KSCreenW/3.0, SECTIONHEIGHT)];
    _planbLab.text = [NSString stringWithFormat:@"今日计划:%d",_allcount];
    _planbLab.textColor = blackFontColor;
    _planbLab.backgroundColor = [UIColor whiteColor];
    endLabel = _planbLab;
    [ViewTool setLableFont13:_planbLab];
    _planbLab.textAlignment = NSTextAlignmentCenter;
    [_planNumView addSubview:_planbLab];
    _completedLab = [[UILabel alloc] initWithFrame:CGRectMake(KSCreenW/3.0,0, KSCreenW/3.0, SECTIONHEIGHT)];
    _completedLab.text = [NSString stringWithFormat:@"已完成:%d",_yicount];
    _completedLab.textColor = blackFontColor;
    _completedLab.backgroundColor = grayLineColor;
    [ViewTool setLableFont13:_completedLab];
    _completedLab.textAlignment = NSTextAlignmentCenter;
    [_planNumView addSubview:_completedLab];
    _notCompletedLab = [[UILabel alloc] initWithFrame:CGRectMake(KSCreenW/3.0*2,0, KSCreenW/3.0, SECTIONHEIGHT)];
    _notCompletedLab.text = [NSString stringWithFormat:@"未完成:%d",_daicount];
    _notCompletedLab.textColor = blackFontColor;
    _notCompletedLab.backgroundColor = grayLineColor;
    [ViewTool setLableFont13:_notCompletedLab];
    _notCompletedLab.textAlignment = NSTextAlignmentCenter;
    [_planNumView addSubview:_notCompletedLab];
    
    [self.view addSubview:_planNumView];
    
    sectionLine1 = [[UIView alloc] initWithFrame:CGRectMake(_planbLab.maxX, 6, 1, _planNumView.height - 12)];
    sectionLine1.backgroundColor = grayListColor;
    sectionLine1.hidden = YES;
    [_planNumView addSubview:sectionLine1];
    
    sectionLine2 = [[UIView alloc] initWithFrame:CGRectMake(_completedLab.maxX, 6, 1, _planNumView.height - 12)];
    sectionLine2.backgroundColor = grayListColor;
    [_planNumView addSubview:sectionLine2];
    
    _splitLine = [[UILabel alloc] initWithFrame:CGRectMake(0, _planNumView.maxY-1, KSCreenW, 1)];
    _splitLine.layer.borderWidth = 10;
    _splitLine.alpha = 0;
    [self.view addSubview:_splitLine];
    
    UIButton *planButt = [[UIButton alloc] init];
    planButt.frame = _planbLab.frame;
    endButt = planButt;
    planButt.tag = -1;
    [planButt addTarget:self action:@selector(planNumViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_planNumView addSubview:planButt];
    
    UIButton *completedButt = [[UIButton alloc] init];
    completedButt.frame = _completedLab.frame;
    completedButt.tag = 1;
    [completedButt addTarget:self action:@selector(planNumViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_planNumView addSubview:completedButt];
    
    UIButton *notCompletedButt = [[UIButton alloc] init];
    notCompletedButt.frame = _notCompletedLab.frame;
    notCompletedButt.tag = 0;
    [notCompletedButt addTarget:self action:@selector(planNumViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_planNumView addSubview:notCompletedButt];
}

- (void)planNumViewButtonClick:(UIButton *)butt
{
    [_visitTableView setContentOffset:CGPointMake(0,0) animated:NO];
    [_otherVisitTableView setContentOffset:CGPointMake(0,0) animated:NO];
    
    if (butt != endButt) {
        [SVProgressHUD showWithStatus:@"加载中"];
        endButt = butt;
    }
    
    if (butt.tag == -1) {
        sectionLine1.hidden = YES;
        sectionLine2.hidden = NO;
        if (endLabel == _planbLab) {
            return;
        }
        _planbLab.backgroundColor = [UIColor whiteColor];
        endLabel.backgroundColor = grayLineColor;
        endLabel = _planbLab;
    }
    else if (butt.tag == 1) {
        sectionLine2.hidden = YES;
        sectionLine1.hidden = YES;
        if (endLabel == _completedLab) {
            return;
        }
        _completedLab.backgroundColor = [UIColor whiteColor];
        endLabel.backgroundColor = grayLineColor;
        
        endLabel = _completedLab;
    }
    else if (butt.tag == 0) {
        sectionLine2.hidden = YES;
        sectionLine1.hidden = NO;
        if (endLabel == _notCompletedLab) {
            return;
        }
        _notCompletedLab.backgroundColor = [UIColor whiteColor];
        endLabel.backgroundColor = grayLineColor;
        endLabel = _notCompletedLab;
    }
    statuIndex = (int)butt.tag;
    [self reloadData];
}
- (void)addTableView{
    _visitTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH- _splitLine.maxY-84) style:UITableViewStylePlain];
    _visitTableView.delegate=self;
    _visitTableView.dataSource=self;
    _visitTableView.tag = 0;
    _visitTableView.backgroundColor = graySectionColor;
    _visitTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_visitTableView];
    _visitTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    _visitTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self reloadmoreData];
        
    }];
    
    if (_isManager) {
        _otherVisitTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH- _splitLine.maxY-84) style:UITableViewStylePlain];
        _otherVisitTableView.delegate=self;
        _otherVisitTableView.dataSource=self;
        _otherVisitTableView.tag = 3;
        _otherVisitTableView.backgroundColor = graySectionColor;
        _otherVisitTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_otherVisitTableView];
        _otherVisitTableView.hidden = YES;
        
        _otherVisitTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self reloadData];
        }];
        _otherVisitTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self reloadmoreData];
        }];
    }
}

//#pragma mark --NewVisitView delagte
- (void)refreshData{
    if (_isManager & !_otherVisitTableView.hidden){
        _planbLab.text = [NSString stringWithFormat:@"今日计划:%d",_allcount2];
        _completedLab.text = [NSString stringWithFormat:@"已完成:%d",_yicount2];
        _notCompletedLab.text = [NSString stringWithFormat:@"未完成:%d",_daicount2];
        if (_allcount2 == 0){
            [self.view makeToast:@"无拜访记录"];
        }
//        NSLog(@"--------==========- -     %@",_daylist);
        [_otherVisitTableView reloadData];
    }else{
        _planbLab.text = [NSString stringWithFormat:@"今日计划:%d",_allcount];
        _completedLab.text = [NSString stringWithFormat:@"已完成:%d",_yicount];
        _notCompletedLab.text = [NSString stringWithFormat:@"未完成:%d",_daicount];
        if (_allcount == 0){
            [self.view makeToast:@"无拜访记录"];
        }
        [_visitTableView reloadData];
    }
}

- (void)changeHeight{
    _planNumView.frame = CGRectMake(0, self.calendarContentView.y+self.calendar.height, KSCreenW, 30);
    _splitLine.frame = CGRectMake(LEFTWIDTH, _planNumView.maxY-1, KSCreenW-LEFTWIDTH*2, 1);
    _visitTableView.frame = CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH-_splitLine.maxY-10-TabbarH);
    _otherVisitTableView.frame = CGRectMake(0, _splitLine.maxY, KSCreenW, KSCreenH-_splitLine.maxY-10-TabbarH);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadmoreData{
    //    pagenum ++;
    rowsnum = rowsnum+10;
    if (_isManager & !_otherVisitTableView.hidden){
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_SUB_VISIT_URL withArray:nil status:statuIndex];
    }else{
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_VISIT_URL withArray:nil status:statuIndex];
    }
    if (_isManager & !_otherVisitTableView.hidden){
        [self getMonthListWithURL:GET_MONTH_SUB_VISIT_URL withYear:_year withMon:_month];
    }else{
        [self getMonthListWithURL:GET_MONTH_VISIT_URL withYear:_year withMon:_month];
    }
}

- (void)reloadData{
    pagenum = 1;
    rowsnum = 10;
    if (_isManager & !_otherVisitTableView.hidden){
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_SUB_VISIT_URL withArray:nil status:statuIndex];
    }else{
        [self getDaylist:_date getPage:pagenum rowsnum:rowsnum withURL:GET_VISIT_URL withArray:nil status:statuIndex];
    }
    if (_isManager & !_otherVisitTableView.hidden){
        [self getMonthListWithURL:GET_MONTH_SUB_VISIT_URL withYear:_year withMon:_month];
    }else{
        [self getMonthListWithURL:GET_MONTH_VISIT_URL withYear:_year withMon:_month];
    }
}

- (void)getMonthListWithURL:(NSString *)url withYear:(NSString *)year withMon:(NSString *)month{
    
    //GET_MONTH_VISIT_URL  GET_MONTH_SUB_VISIT_URL
    NSDictionary * params1 = [NSDictionary dictionaryWithObjectsAndKeys:@(UID), @"uid", TOKEN, @"token",year, @"year", month, @"month", nil];
    
    if ( _visitTableView.hidden == YES && kid.length != 0 ) {
        params1 = [NSDictionary dictionaryWithObjectsAndKeys:@(UID), @"uid", TOKEN, @"token",year, @"year", month, @"month",kid, @"ids", nil];
    }
    
    AFHTTPRequestOperationManager * manager1 = [AFHTTPRequestOperationManager manager];
    manager1.requestSerializer.timeoutInterval = 20;
    [manager1 POST:url parameters:params1 success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"%@: getMonthlist   返回 %@",url,params1);
        int code = [[(NSDictionary *)responseObject objectForKey:@"code"] intValue];
        if(code != 100)
        {
            NSString * message = [(NSDictionary *)responseObject objectForKey:@"message"];
            [self.view makeToast:message];
          
            [_visitTableView.mj_header endRefreshing];
            [_visitTableView.mj_footer endRefreshing];
            [clander setremaindArr:nil];
            
        }else{
            NSArray *times = [(NSDictionary *)responseObject objectForKey:@"timelist"];
            
            [_timelist1 removeAllObjects];
            [_timelist2 removeAllObjects];
            if (_otherVisitTableView.hidden == YES) {
                [self addMonTimeList:times toTimeList:_timelist1];
                
                [clander setremaindArr:_timelist1];
                
            }else{
                [self addMonTimeList:times toTimeList:_timelist2];
                
                [clander setremaindArr:_timelist2];
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        [self.view hideToastActivity];
        [_visitTableView.mj_header endRefreshing];
        [_otherVisitTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
        [self.view makeToast:error.localizedDescription];
    }];
}

- (void)getDaylist:(NSDate *)date getPage:(int)page rowsnum:(int)rows withURL:(NSString *)url withArray:(NSArray *)array status:(int)status{
    NSDictionary * params;
    params = [NSDictionary dictionaryWithObjectsAndKeys:@(UID), @"uid", TOKEN, @"token", _yearstart, @"year", _monthstart, @"month",_daystart, @"day", _year, @"year2", _month, @"month2", _day, @"day2",@(rows),@"rows",@(page),@"page",@(status),@"status",nil];
  
    if (_visitTableView.hidden == YES  && kid.length != 0) {
        params = [NSDictionary dictionaryWithObjectsAndKeys:@(UID), @"uid", TOKEN, @"token", _yearstart, @"year", _monthstart, @"month",_daystart, @"day", _year, @"year2", _month, @"month2", _day, @"day2",@(rows),@"rows",@(page),@"page",@(status),@"status",kid, @"ids",nil];
    }
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    manager.requestSerializer.timeoutInterval = 20;

    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view hideToastActivity];
        NSLog(@"%@: getDaylist   返回 : %@",params,responseObject);
        int code = [[(NSDictionary *)responseObject objectForKey:@"code"] intValue];
        
        if(code != 100)
        {
            NSString * message = [(NSDictionary *)responseObject objectForKey:@"message"];
            [self.view makeToast:message];
            if ([message isEqualToString:@"没有数据"]) {
                if ([url isEqualToString:GET_VISIT_URL]) {
                    _allcount = 0;
                    _yicount = 0;
                    _daicount = 0;
                    _daylist = nil;
                }else{
                    _allcount2 = 0;
                    _yicount2 = 0;
                    _daicount2 = 0;
                    _daylist2 = nil;
                }
            }
        }else{
            if ([url isEqualToString:GET_VISIT_URL]) {
                _allcount = [[(NSDictionary *)responseObject objectForKey:@"allcount"] intValue];
                _yicount = [[(NSDictionary *)responseObject objectForKey:@"yicount"] intValue];
                _daicount = _allcount - _yicount;
                _daylist = [(NSDictionary *)responseObject objectForKey:@"daylist"];
            }else{
                _allcount2 = [[(NSDictionary *)responseObject objectForKey:@"allcount"] intValue];
                _yicount2 = [[(NSDictionary *)responseObject objectForKey:@"yicount"] intValue];
                _daicount2 = _allcount2 - _yicount2;;
                _daylist2 = [(NSDictionary *)responseObject objectForKey:@"daylist"];
            }
        }
        
        [self refreshData];
                
        [_visitTableView.mj_header endRefreshing];
        [_otherVisitTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
        if (_isManager == YES) {
            
            if (_allcount == _daylist.count || _yicount == _daylist.count || _daicount == _daylist.count) {
                [_visitTableView.mj_footer endRefreshingWithNoMoreData];
                [_otherVisitTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                [_visitTableView.mj_footer endRefreshing];
                [_otherVisitTableView.mj_footer endRefreshing];
            }
            [_visitTableView scrollsToTop];
        }
        else{
            
            if (_allcount2 == _daylist2.count || _yicount2 == _daylist2.count || _daicount2 == _daylist2.count) {
                [_visitTableView.mj_footer endRefreshingWithNoMoreData];
                [_otherVisitTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [_otherVisitTableView.mj_footer endRefreshing];
                [_visitTableView.mj_footer endRefreshing];
            }
            [_otherVisitTableView scrollsToTop];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view makeToast:error.localizedDescription];
        [self.view hideToastActivity];
        [_visitTableView.mj_header endRefreshing];
        [_otherVisitTableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
}

- (void)addMonTimeList:(NSArray *)array toTimeList:(NSMutableArray *)timeList{
    for (NSString *time in array) {
        if (![timeList containsObject:time]) {
            [timeList addObject:time];
        }
    }
//    NSLog(@"%@*-*-*-*-*",timeList);
}

- (void)clickToNewVisit{
    NSLog(@"clickToNewVisit");
    CATransition* transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;//可更改为其他方式
    transition.subtype = kCATransitionFromTop;//可更改为其他方式
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    
    NewVisitViewController *newVisitViewController = [[NewVisitViewController alloc] init];
    newVisitViewController.delegate = self;
    [self.navigationController pushViewController:newVisitViewController animated:NO];
    
}

- (void)clickToselectUser{
    CATransition* transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;//可更改为其他方式
    transition.subtype = kCATransitionFromRight;//可更改为其他方式
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    
    SelectPersonViewController *selectViewController = [[SelectPersonViewController alloc]init];
    selectViewController.modalPresentationStyle =  UIModalPresentationPageSheet;
    selectViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    selectViewController.delegate = self;
    [self.navigationController pushViewController:selectViewController animated:NO];
}
#pragma mark - SelectViewController-Delegate
//SelectStaffViewDelegate
- (void)getSelectedStaff:(NSArray *)array{
    NSMutableArray *ids = [NSMutableArray array];
//    NSLog(@"%@************",array);
    kid = @"";
    for (int i = 0; i < array.count; i ++) {
            UserModel *user = [array objectAtIndex:i];
        if (array.count ==1) {
            kid = [NSString stringWithFormat:@"%d",user.uid];
        }else{
            
            kid = [NSString stringWithFormat:@"%@%d,",kid,user.uid];
        }
    }
    NSLog(@"%@************",kid);
    
    [self reloadData];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (tableView == _visitTableView){
        return _daylist.count;
        
    }else{
        return _daylist2.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELLHEIGHT2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    VisitTableViewCell *cell = (VisitTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //    cell.delegate = self;//位置写错了。。
    if (cell == nil) {
        cell = [[VisitTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    if (tableView == _visitTableView) {
        
        cell.companyLab.text = [DataTool changeType:[_daylist[indexPath.row] objectForKey:@"company"]];
//        NSLog(@"公司名称 ----- %@",[_daylist[indexPath.row] objectForKey:@"company"]);
        if (cell.companyLab.text.length != 0) {
            CGFloat W = [cell.companyLab.text boundingRectWithSize:CGSizeMake(MAXFLOAT, cell.companyLab.height) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : cell.companyLab.font,NSForegroundColorAttributeName : blackFontColor} context:nil].size.width;
            
            cell.companyLevelImage.frame = CGRectMake(W + 10, cell.companyLab.y, 20, 20);

        }
        
        cell.addressLab.text = [_daylist[indexPath.row] objectForKey:@"address"];
        
        
        int visitStaus = [[_daylist[indexPath.row] objectForKey:@"status"] intValue];
        
        if (visitStaus == 1) {
            cell.visitImage.image = [UIImage imageNamed:@"HY_list_icon_visited"];
            
            cell.visitStausLab.text = @"";
            cell.visitTimeLab.text = @"";
        }
        
        else if (visitStaus == 0){
            
            cell.visitStausLab.text = @"待拜访";
            cell.visitStausLab.textColor = redBackColor;
            cell.visitImage.image = [UIImage imageNamed:@""];
        }else if (visitStaus == 2){
            cell.visitStausLab.text = @"未拜访";
            cell.visitStausLab.textColor = grayFontColor;
            cell.visitImage.image = [UIImage imageNamed:@""];
        }else if (visitStaus == 3){
            cell.visitStausLab.text = @"拜访中";
            cell.visitStausLab.textColor = greenBackColor;
            cell.visitImage.image = [UIImage imageNamed:@""];
        }
        
        cell.delegate = self;
    }else{
        
        if (_daylist2.count != 0) {
            cell.companyLab.text = [_daylist2[indexPath.row] objectForKey:@"company"];
            
            CGFloat W = [cell.companyLab.text boundingRectWithSize:CGSizeMake(MAXFLOAT, cell.companyLab.height) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : cell.companyLab.font,NSForegroundColorAttributeName : blackFontColor} context:nil].size.width;
            cell.companyLevelImage.frame = CGRectMake(W + 10, cell.companyLab.y, 20, 20);
            
            cell.addressLab.text = [_daylist2[indexPath.row] objectForKey:@"address"];
            
            int visitStaus = [[_daylist2[indexPath.row] objectForKey:@"status"] intValue];
            
            if (visitStaus == 1) {
                cell.visitImage.image = [UIImage imageNamed:@"HY_list_icon_visited"];
                //cell.visitStausLab2.text = @"已拜访";
                cell.visitStausLab.text = @"";
                cell.visitTimeLab.text = @"";
            }
            else if (visitStaus == 0){
                //cell.visitImage.image = [UIImage imageNamed:@"HY_list_icon_visited"];
                cell.visitImage.image = [UIImage imageNamed:@""];
                cell.visitStausLab.text = @"未拜访";
                cell.visitStausLab.textColor = grayFontColor;
            }
            else{
                cell.visitImage.image = [UIImage imageNamed:@""];
                cell.visitStausLab.text = @"待拜访";
                cell.visitStausLab.textColor = grayFontColor;
            }
            cell.delegate = self;
        }
    }
    return cell;
}
- (void)jumpControllerToMap:(NSString *)btnTitle
{
    MapClientViewController *mapClient = [[MapClientViewController alloc] init];
    [self.navigationController pushViewController:mapClient animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    NSLog(@"------------------- 7878787878       %@",[_daylist[indexPath.row] objectForKey:@"cid"]);
    if (tableView == _visitTableView) {
        VisitDetailViewController *visitDetailViewController = [[VisitDetailViewController alloc] init];
        
        [self.navigationController pushViewController:visitDetailViewController animated:YES];
        visitDetailViewController.visitId = [[_daylist[indexPath.row] objectForKey:@"id"] intValue];
        visitDetailViewController.canChangeText = YES;
        
    }else{
        VisitDetailViewController *visitDetailViewController = [[VisitDetailViewController alloc] init];
        
        [self.navigationController pushViewController:visitDetailViewController animated:YES];
        visitDetailViewController.visitId = [[_daylist2[indexPath.row] objectForKey:@"id"] intValue];
        visitDetailViewController.canChangeText = NO;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.y > _planNumView.y) {//如果当前位移大于缓存位移，说明scrollView向上滑动
        
    }
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    lastContentOffset = scrollView.contentOffset.y;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    
    if (lastContentOffset < scrollView.contentOffset.y) {
        NSLog(@"向上滚动");
    }else{
        NSLog(@"向下滚动");
    }
}

@end